﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyKho
{
    public partial class frmVatTuHangHoa : Form
    {
        private TaiKhoan taiKhoan = new TaiKhoan("admin", "admin", "admin");
        private DataSet dsDanhSachVatTuHangHoa, dsDonViTinh;
        private string[] danhSachXoaVatTu, danhSachXoaDonViTinh;
        private int sttDanhSachXoaVatTu, sttdanhSachXoaDonViTinh;
        private bool flag;

        public frmVatTuHangHoa()
        {
            InitializeComponent();
        }

        private void gct_MouseClick(object sender, MouseEventArgs e)
        {
            int[] itemChon;
            if (sender == gctVatTuHangHoa)
            {
                itemChon = gvVatTuHangHoa.GetSelectedRows();
                hienThiDonViTinh(gvVatTuHangHoa.GetDataRow(itemChon[0])[0].ToString());
                flag = true;
            }
            else
            {
                itemChon = gvDonViTinh.GetSelectedRows();
                flag = false;
            }
            if (e.Button == MouseButtons.Right)
            {
                try
                {
                    if (!gvVatTuHangHoa.IsNewItemRow(itemChon[0]) && !gvDonViTinh.IsNewItemRow(itemChon[0]))
                    {
                        ContextMenu m = new ContextMenu();
                        m.MenuItems.Add(new MenuItem("&Xóa", menuXoa));
                        m.Show((sender as Control), new Point(e.X, e.Y));
                    }
                }
                catch { }
            }
        }

        private void menuXoa(object sender, EventArgs e)
        {
            if (flag)//Danh sách vật tư
            {
                btnSuaDanhSachVatTu.Visible = false;
                btnNapLaiDanhSachVatTu.Visible = btnLuuDanhSachVatTu.Visible = true;
                int[] itemChon = gvVatTuHangHoa.GetSelectedRows();
                danhSachXoaVatTu[sttDanhSachXoaVatTu] = gvVatTuHangHoa.GetDataRow(itemChon[0])["MaVatTu"].ToString();
                sttDanhSachXoaVatTu++;
                gvVatTuHangHoa.DeleteRow(itemChon[0]);
            }
            else//Đơn vị tính
            {
                btnSuaDonViTinh.Visible = false;
                btnNapLaiDonViTinh.Visible = btnLuuDonViTinh.Visible = true;
                int[] itemChon = gvDonViTinh.GetSelectedRows();
                danhSachXoaDonViTinh[sttdanhSachXoaDonViTinh] = gvDonViTinh.GetDataRow(itemChon[0])["STT"].ToString();
                sttdanhSachXoaDonViTinh++;
                gvDonViTinh.DeleteRow(itemChon[0]);
            }
        }

        private void hienThiVatTuHangHoa()
        {
            try
            {
                btnSuaDanhSachVatTu.Visible = true;
                gvVatTuHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gclMaVatTu.OptionsColumn.AllowEdit = gclTenVatTu.OptionsColumn.AllowEdit = false;
                if (gvVatTuHangHoa.Columns.Count > 2)
                {
                    gvVatTuHangHoa.Columns.Remove(gvVatTuHangHoa.Columns[2]);
                    gctVatTuHangHoa.RepositoryItems.Remove(gctVatTuHangHoa.RepositoryItems[2]);
                }

                string sql = "select * from func_getDanhSachVatTuHangHoa()";
                dsDanhSachVatTuHangHoa = connection.FillDataSet(sql,CommandType.StoredProcedure, taiKhoan);
                
                gctVatTuHangHoa.DataSource = dsDanhSachVatTuHangHoa.Tables[0];
                gclMaVatTu.FieldName = dsDanhSachVatTuHangHoa.Tables[0].Columns["MaVatTu"].ColumnName;
                gclTenVatTu.FieldName = dsDanhSachVatTuHangHoa.Tables[0].Columns["TenVatTu"].ColumnName;

                danhSachXoaVatTu = new string[dsDanhSachVatTuHangHoa.Tables[0].Rows.Count];
                sttDanhSachXoaVatTu = 0;
            }
            catch { MessageBox.Show("Không thể cập nhật danh sách vật tư hàng hóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void frmVatTuHangHoa_Load(object sender, EventArgs e)
        {
            hienThiVatTuHangHoa();
            hienThiDonViTinh(gvVatTuHangHoa.GetDataRow(0)[0].ToString());
        }

        private void btnSuaDanhSachVatTu_CheckedChanged(object sender, EventArgs e)
        {
            if (btnSuaDanhSachVatTu.Checked)
            {
                // 
                // txtDonViTinh
                //
                RepositoryItemTextEdit txtDonViTinh = new RepositoryItemTextEdit();
                ((System.ComponentModel.ISupportInitialize)(txtDonViTinh)).BeginInit();
                txtDonViTinh.AutoHeight = false;
                txtDonViTinh.Name = "txtDonViTinh";
                // 
                // gclDonViTinhMacDinh
                // 
                GridColumn gclDonViTinhMacDinh = new GridColumn();
                gclDonViTinhMacDinh.Caption = "Đơn vị tính";
                gclDonViTinhMacDinh.ColumnEdit = txtDonViTinh;
                gclDonViTinhMacDinh.Name = "gclDonViTinhMacDinh";
                gclDonViTinhMacDinh.Visible = true;
                gclDonViTinhMacDinh.VisibleIndex = 2;
                //
                //
                //
                gctVatTuHangHoa.RepositoryItems.AddRange(new RepositoryItem[] { txtDonViTinh });
                gvVatTuHangHoa.Columns.AddRange(new GridColumn[] { gclDonViTinhMacDinh });
                //
                //
                //
                gclDonViTinhMacDinh.FieldName = dsDanhSachVatTuHangHoa.Tables[0].Columns["DonViTinh"].ColumnName;

                gvVatTuHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
                gclMaVatTu.OptionsColumn.AllowEdit = gclTenVatTu.OptionsColumn.AllowEdit = true;
                btnSuaDanhSachVatTu.Visible = btnSuaDanhSachVatTu.Checked = false;
                btnNapLaiDanhSachVatTu.Visible = btnLuuDanhSachVatTu.Visible = true;
            }
        }

        private void btnNapLai_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == btnNapLaiDanhSachVatTu)
            {
                if (btnNapLaiDanhSachVatTu.Checked)
                {
                    hienThiVatTuHangHoa();
                    btnNapLaiDanhSachVatTu.Checked = btnLuuDanhSachVatTu.Visible = btnNapLaiDanhSachVatTu.Visible = false;
                }
            }
            else
                if (btnNapLaiDonViTinh.Checked)
                {
                    hienThiDonViTinh(gvVatTuHangHoa.GetDataRow(0)[0].ToString());
                    btnNapLaiDonViTinh.Checked = btnLuuDonViTinh.Visible = btnNapLaiDonViTinh.Visible = false;
                }
        }

        private void btnXoa_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == btnXoaDanhSachVatTu)
            {
                if (btnXoaDanhSachVatTu.Checked)
                {
                    flag = true;
                    menuXoa(sender, e);
                    btnXoaDanhSachVatTu.Checked = false;
                }
            }
            else
                if (btnXoaDonViTinh.Checked)
                {
                    flag = false;
                    menuXoa(sender, e);
                    btnXoaDonViTinh.Checked = false;
                }
        }

        private void btnLuu_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == btnLuuDanhSachVatTu)
            {
                if (btnLuuDanhSachVatTu.Checked)
                {
                    capNhatDanhSachVatTu(dsDanhSachVatTuHangHoa.Tables[0]);
                    btnLuuDanhSachVatTu.Checked = false;
                    btnNapLaiDanhSachVatTu.Checked = true;
                }
            }
            else
                if (btnLuuDonViTinh.Checked)
                {
                    capNhatDonViTinh(dsDonViTinh.Tables[0]);
                    btnLuuDonViTinh.Checked = false;
                    btnNapLaiDonViTinh.Checked = true;
                }
        }

        private void capNhatDanhSachVatTu(DataTable dt)
        {
            if (dt.GetChanges() != null)
            {
                for (int i = 0; i < danhSachXoaVatTu.Length; i++)
                {
                    if (danhSachXoaVatTu[i] != null)
                    {
                        string[] para = { "@MaVatTu" };
                        string[] values = { danhSachXoaVatTu[i] };

                        connection.Excute("proc_deleteVatTuHangHoa", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                }

                foreach (DataRow dr in dt.GetChanges().Rows)
                {
                    

                    if (dr.RowState == DataRowState.Added)
                    {
                        string[] para = { "@MaVatTu", "@TenVatTu", "@DonViTinh" };
                        string[] values = { dr["MaVatTu"].ToString(), dr["TenVatTu"].ToString(), dr["DonViTinh"].ToString() };

                        connection.Excute("proc_insertVatTuHangHoa", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                    else
                        if (dr.RowState == DataRowState.Modified)
                        {
                            string[] para = { "@MaVatTu", "@TenVatTu" };
                            string[] values = { dr["MaVatTu"].ToString(), dr["TenVatTu"].ToString() };

                            connection.Excute("proc_updateVatTuHangHoa", CommandType.StoredProcedure, para, values, taiKhoan);
                        }
                }
            }
        }

        private void hienThiDonViTinh(string maVatTu)
        {
            try
            {
                btnSuaDonViTinh.Visible = true;
                gvDonViTinh.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gclDonViTinh.OptionsColumn.AllowEdit = gclMacDinh.OptionsColumn.AllowEdit = gclGiaTriQuiDoi.OptionsColumn.AllowEdit = false;

                string[] para = {"@MaVatTu"};
                string[] values = {maVatTu};
                dsDonViTinh = connection.FillDataSet("proc_getDonViTinh", CommandType.StoredProcedure, para, values, taiKhoan);

                gctDonViTinh.DataSource = dsDonViTinh.Tables[0];
                gclDonViTinh.FieldName = dsDonViTinh.Tables[0].Columns["DonViTinh"].ColumnName;
                gclMacDinh.FieldName = dsDonViTinh.Tables[0].Columns["LaMacDinh"].ColumnName;
                gclGiaTriQuiDoi.FieldName = dsDonViTinh.Tables[0].Columns["GiaTriQuiDoi"].ColumnName;

                danhSachXoaDonViTinh = new string[dsDonViTinh.Tables[0].Rows.Count];
                sttdanhSachXoaDonViTinh = 0;
            }
            catch { MessageBox.Show("Không thể cập nhật đơn vị tính","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); }

        }

        private void capNhatDonViTinh(DataTable dt)
        {
            if (dt.GetChanges() != null)
            {
                for (int i = 0; i < danhSachXoaDonViTinh.Length; i++)
                {
                    if (danhSachXoaDonViTinh[i] != null)
                    {
                        string[] para = { "@STT" };
                        string[] values = { danhSachXoaDonViTinh[i] };

                        connection.Excute("proc_deleteDonViTinh", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                }

                foreach (DataRow dr in dt.GetChanges().Rows)
                {
                    

                    if (dr.RowState == DataRowState.Added)
                    {
                        int[] itemChon = gvVatTuHangHoa.GetSelectedRows();
                        string[] para = { "@MaVatTu", "@DonViTinh", "@GiaTriQuiDoi", "@LaMacDinh" };
                        string[] values = { gvVatTuHangHoa.GetDataRow(itemChon[0])["MaVatTu"].ToString(), dr["DonViTinh"].ToString(), dr["GiaTriQuiDoi"].ToString(), dr["LaMacDinh"].ToString() };

                        connection.Excute("proc_insertDonViTinh", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                    else
                        if (dr.RowState == DataRowState.Modified)
                        {

                            string[] para = { "@STT", "@DonViTinh", "@GiaTriQuiDoi", "@LaMacDinh" };
                            string[] values = { dr["STT"].ToString(), dr["DonViTinh"].ToString(), dr["GiaTriQuiDoi"].ToString(), dr["LaMacDinh"].ToString() };

                            connection.Excute("proc_updateDonViTinh", CommandType.StoredProcedure, para, values, taiKhoan);
                        }
                }
            }
        }

        private void btnSuaDonViTinh_CheckedChanged(object sender, EventArgs e)
        {
            if (btnSuaDonViTinh.Checked)
            {
                gvDonViTinh.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
                gclDonViTinh.OptionsColumn.AllowEdit = gclMacDinh.OptionsColumn.AllowEdit = gclGiaTriQuiDoi.OptionsColumn.AllowEdit = true;
                btnNapLaiDonViTinh.Visible = btnLuuDonViTinh.Visible = true;
                btnSuaDonViTinh.Visible = btnSuaDonViTinh.Checked = false;
            }
        }

        private void gvDonViTinh_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            if (gclMacDinh == e.Column && !gvDonViTinh.IsNewItemRow(e.RowHandle))
            {
                if (gvDonViTinh.GetDataRow(e.RowHandle)["LaMacDinh"].ToString() == "True")
                {
                    for (int i = 0; i < gvDonViTinh.RowCount; i++)
                    {
                        if (i == e.RowHandle)
                            continue;
                        gvDonViTinh.SetRowCellValue(i, gclMacDinh, false);
                    }
                }
            }
        }

    }
}
